/*
 * @Date: 2020-09-16 14:30:46
 * @LastEditors: 小枫
 * @LastEditTime: 2020-09-16 20:09:54
 * @FilePath: \feng-blog\app\controller\Review.ts
 */
import { Controller } from 'egg';

export default class ReviewController extends Controller {
  public async index() {
    const { ctx } = this;
    const { topic_id } = ctx.params;
    try {
      const result = await ctx.service.review.getReviews(topic_id);
      ctx.status = 200;
      ctx.body = {
        data: result,
      };
    } catch (error) {
      ctx.status = 502;
      ctx.body = {
        msg: error,
      };
    }
  }
  public async create() {
    const { ctx } = this;
    let { topic_id, content, createAt, to_uid } = ctx.request.body;
    to_uid = to_uid ? to_uid : 0;
    const from_uid = ctx.authUser;
    const review = {
      topic_id,
      content,
      from_uid,
      to_uid,
      createAt,
    };
    try {
      const result = await ctx.service.review.insertReview(review);
      ctx.status = 200;
      ctx.body = {
        data: result,
      };
    } catch (error) {
      // console.log(error);
      ctx.status = 502;
      ctx.body = {
        msg: error,
      };
    }
  }
}
